{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0d99fc7f",
   "metadata": {},
   "source": [
    "## 1. APP 现货交易-基础订单函数总览"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "432486f7",
   "metadata": {},
   "source": [
    "|方法|解释|\n",
    "|:---|:---|\n",
    "|set_order|下单（API原始接口）|\n",
    "|get_order|查询订单（API原始接口）|\n",
    "|get_orders_pending|查看当前挂单|\n",
    "|get_orders_pending_open|查看当前购买挂单|\n",
    "|get_orders_pending_close|查看当前卖出挂单|\n",
    "|wait_order_FILLED|等待订单成交|\n",
    "|cancel_order|撤销订单（API原始接口）|"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f6a18f4e",
   "metadata": {},
   "source": [
    "## 2. 模型实例化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "7f4643f4",
   "metadata": {},
   "outputs": [],
   "source": [
    "from binance_interface.app import BinanceSPOT\n",
    "from binance_interface.app.utils import eprint\n",
    "import paux.date"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f3db3bb0",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 转发：需搭建转发服务器，可参考：https://github.com/pyted/binance_resender\n",
    "proxy_host = None\n",
    "key = 'xxxx'\n",
    "secret = 'xxxx'\n",
    "\n",
    "binanceSPOT = BinanceSPOT(\n",
    "    key=key,\n",
    "    secret=secret,\n",
    "    proxy_host=proxy_host,\n",
    "    timezone='Asia/Shanghai',\n",
    ")\n",
    "trade = binanceSPOT.trade"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fd0f86f5",
   "metadata": {},
   "source": [
    "## 3. 下单（API原始接口） set_order"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "98bee0cf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': {'symbol': 'MANAUSDT',\n",
      "          'orderId': 2207382482,\n",
      "          'orderListId': -1,\n",
      "          'clientOrderId': 'P3PQXqOCmLmvSgT3bHpNhY',\n",
      "          'transactTime': 1706119355892,\n",
      "          'price': '0.40000000',\n",
      "          'origQty': '15.00000000',\n",
      "          'executedQty': '0.00000000',\n",
      "          'cummulativeQuoteQty': '0.00000000',\n",
      "          'status': 'NEW',\n",
      "          'timeInForce': 'GTC',\n",
      "          'type': 'LIMIT',\n",
      "          'side': 'BUY',\n",
      "          'workingTime': 1706119355892,\n",
      "          'fills': [],\n",
      "          'selfTradePreventionMode': 'EXPIRE_MAKER'},\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "set_order_result = trade.set_order(\n",
    "    symbol='MANAUSDT',\n",
    "    price='0.4',\n",
    "    quantity='15',\n",
    "    side='BUY',\n",
    "    type='LIMIT',\n",
    "    timeInForce='GTC',\n",
    ")\n",
    "eprint(set_order_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "62c9da01",
   "metadata": {},
   "source": [
    "## 4. 查询订单（API原始接口） get_order"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "893158b6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': {'symbol': 'MANAUSDT',\n",
      "          'orderId': 2207382482,\n",
      "          'orderListId': -1,\n",
      "          'clientOrderId': 'P3PQXqOCmLmvSgT3bHpNhY',\n",
      "          'price': '0.40000000',\n",
      "          'origQty': '15.00000000',\n",
      "          'executedQty': '0.00000000',\n",
      "          'cummulativeQuoteQty': '0.00000000',\n",
      "          'status': 'NEW',\n",
      "          'timeInForce': 'GTC',\n",
      "          'type': 'LIMIT',\n",
      "          'side': 'BUY',\n",
      "          'stopPrice': '0.00000000',\n",
      "          'icebergQty': '0.00000000',\n",
      "          'time': 1706119355892,\n",
      "          'updateTime': 1706119355892,\n",
      "          'isWorking': True,\n",
      "          'workingTime': 1706119355892,\n",
      "          'origQuoteOrderQty': '0.00000000',\n",
      "          'selfTradePreventionMode': 'EXPIRE_MAKER'},\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "orderId = set_order_result['data']['orderId']\n",
    "order_result = trade.get_order(\n",
    "    symbol='MANAUSDT',\n",
    "    orderId=orderId,\n",
    ")\n",
    "eprint(order_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e7dc4b6e",
   "metadata": {},
   "source": [
    "## 5. 查看当前挂单 get_orders_pending"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "3424cbf7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': [{'symbol': 'MANAUSDT',\n",
      "           'orderId': 2207181640,\n",
      "           'orderListId': -1,\n",
      "           'clientOrderId': 'm7UdoWyzjkl80q1rgzJ19U',\n",
      "           'price': '1.00000000',\n",
      "           'origQty': '5.00000000',\n",
      "           'executedQty': '0.00000000',\n",
      "           'cummulativeQuoteQty': '0.00000000',\n",
      "           'status': 'NEW',\n",
      "           'timeInForce': 'GTC',\n",
      "           'type': 'LIMIT',\n",
      "           'side': 'SELL',\n",
      "           'stopPrice': '0.00000000',\n",
      "           'icebergQty': '0.00000000',\n",
      "           'time': 1706105539772,\n",
      "           'updateTime': 1706105539772,\n",
      "           'isWorking': True,\n",
      "           'workingTime': 1706105539772,\n",
      "           'origQuoteOrderQty': '0.00000000',\n",
      "           'selfTradePreventionMode': 'EXPIRE_MAKER'},\n",
      "          {'symbol': 'MANAUSDT',\n",
      "           'orderId': 2207182516,\n",
      "           'orderListId': -1,\n",
      "           'clientOrderId': 'G03pZe5KataOJ7C1DS3gmn',\n",
      "           'price': '0.47940000',\n",
      "           'origQty': '15.00000000',\n",
      "           'executedQty': '0.00000000',\n",
      "           'cummulativeQuoteQty': '0.00000000',\n",
      "           'status': 'NEW',\n",
      "           'timeInForce': 'GTC',\n",
      "           'type': 'LIMIT',\n",
      "           'side': 'SELL',\n",
      "           'stopPrice': '0.00000000',\n",
      "           'icebergQty': '0.00000000',\n",
      "           'time': 1706105588896,\n",
      "           'updateTime': 1706105588896,\n",
      "           'isWorking': True,\n",
      "           'workingTime': 1706105588896,\n",
      "           'origQuoteOrderQty': '0.00000000',\n",
      "           'selfTradePreventionMode': 'EXPIRE_MAKER'}],\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "# 查询symbol 从start到end全部未成交订单\n",
    "orders_pending_result = trade.get_orders_pending(\n",
    "    symbol='',  # 默认为空，表示全部产品\n",
    "    start='2024-01-23 10:00:00',  # 默认为空，表示不限定订单的起始时间\n",
    "    end='2024-01-25',  # 默认为空，表示不限定订单的终止时间\n",
    ")\n",
    "eprint(orders_pending_result, length=30)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "0401f964",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': [{'symbol': 'BTCUSDT',\n",
      "           'orderId': 24501202294,\n",
      "           'orderListId': -1,\n",
      "           'clientOrderId': 'ios_7952d852422d460e9c82405c628af712',\n",
      "           'price': '38000.00000000',\n",
      "           'origQty': '0.00026000',\n",
      "           'executedQty': '0.00000000',\n",
      "           'cummulativeQuoteQty': '0.00000000',\n",
      "           'status': 'NEW',\n",
      "           'timeInForce': 'GTC',\n",
      "           'type': 'LIMIT',\n",
      "           'side': 'BUY',\n",
      "           'stopPrice': '0.00000000',\n",
      "           'icebergQty': '0.00000000',\n",
      "           'time': 1706119340397,\n",
      "           'updateTime': 1706119340397,\n",
      "           'isWorking': True,\n",
      "           'workingTime': 1706119340397,\n",
      "           'origQuoteOrderQty': '0.00000000',\n",
      "           'selfTradePreventionMode': 'EXPIRE_MAKER'}],\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "# 查询symbol 从start到end全部未成交订单\n",
    "orders_pending_result = trade.get_orders_pending(\n",
    "    symbol='BTCUSDT',  # 默认为空，表示全部产品\n",
    "    start='2024-01-23 10:00:00',  # 默认为空，表示不限定订单的起始时间\n",
    "    end='2024-01-26',  # 默认为空，表示不限定订单的终止时间\n",
    ")\n",
    "eprint(orders_pending_result, length=30)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2c9efea3",
   "metadata": {},
   "source": [
    "## 6 查看当前购买挂单 get_orders_pending_open"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "7132f0fb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': [{'symbol': 'MANAUSDT',\n",
      "           'orderId': 2207382482,\n",
      "           'orderListId': -1,\n",
      "           'clientOrderId': 'P3PQXqOCmLmvSgT3bHpNhY',\n",
      "           'price': '0.40000000',\n",
      "           'origQty': '15.00000000',\n",
      "           'executedQty': '0.00000000',\n",
      "           'cummulativeQuoteQty': '0.00000000',\n",
      "           'status': 'NEW',\n",
      "           'timeInForce': 'GTC',\n",
      "           'type': 'LIMIT',\n",
      "           'side': 'BUY',\n",
      "           'stopPrice': '0.00000000',\n",
      "           'icebergQty': '0.00000000',\n",
      "           'time': 1706119355892,\n",
      "           'updateTime': 1706119355892,\n",
      "           'isWorking': True,\n",
      "           'workingTime': 1706119355892,\n",
      "           'origQuoteOrderQty': '0.00000000',\n",
      "           'selfTradePreventionMode': 'EXPIRE_MAKER'}],\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "orders_pending_open_result = trade.get_orders_pending_open(\n",
    "    symbol='MANAUSDT',\n",
    ")\n",
    "eprint(orders_pending_open_result, length=30)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7f01428a",
   "metadata": {},
   "source": [
    "## 7. 查看当前卖出挂单 get_orders_pending_close"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "28f600c5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': [{'symbol': 'MANAUSDT',\n",
      "           'orderId': 2207181640,\n",
      "           'orderListId': -1,\n",
      "           'clientOrderId': 'm7UdoWyzjkl80q1rgzJ19U',\n",
      "           'price': '1.00000000',\n",
      "           'origQty': '5.00000000',\n",
      "           'executedQty': '0.00000000',\n",
      "           'cummulativeQuoteQty': '0.00000000',\n",
      "           'status': 'NEW',\n",
      "           'timeInForce': 'GTC',\n",
      "           'type': 'LIMIT',\n",
      "           'side': 'SELL',\n",
      "           'stopPrice': '0.00000000',\n",
      "           'icebergQty': '0.00000000',\n",
      "           'time': 1706105539772,\n",
      "           'updateTime': 1706105539772,\n",
      "           'isWorking': True,\n",
      "           'workingTime': 1706105539772,\n",
      "           'origQuoteOrderQty': '0.00000000',\n",
      "           'selfTradePreventionMode': 'EXPIRE_MAKER'},\n",
      "          {'symbol': 'MANAUSDT',\n",
      "           'orderId': 2207182516,\n",
      "           'orderListId': -1,\n",
      "           'clientOrderId': 'G03pZe5KataOJ7C1DS3gmn',\n",
      "           'price': '0.47940000',\n",
      "           'origQty': '15.00000000',\n",
      "           'executedQty': '0.00000000',\n",
      "           'cummulativeQuoteQty': '0.00000000',\n",
      "           'status': 'NEW',\n",
      "           'timeInForce': 'GTC',\n",
      "           'type': 'LIMIT',\n",
      "           'side': 'SELL',\n",
      "           'stopPrice': '0.00000000',\n",
      "           'icebergQty': '0.00000000',\n",
      "           'time': 1706105588896,\n",
      "           'updateTime': 1706105588896,\n",
      "           'isWorking': True,\n",
      "           'workingTime': 1706105588896,\n",
      "           'origQuoteOrderQty': '0.00000000',\n",
      "           'selfTradePreventionMode': 'EXPIRE_MAKER'}],\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "orders_pending_close_result = trade.get_orders_pending_close(\n",
    "    symbol='MANAUSDT',\n",
    ")\n",
    "eprint(orders_pending_close_result, length=30)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c94a721c",
   "metadata": {},
   "source": [
    "## 8. 等待订单成交 wait_order_FILLED"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "1c307207",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': {'symbol': 'MANAUSDT',\n",
      "          'orderId': 2207382482,\n",
      "          'orderListId': -1,\n",
      "          'clientOrderId': 'P3PQXqOCmLmvSgT3bHpNhY',\n",
      "          'price': '0.40000000',\n",
      "          'origQty': '15.00000000',\n",
      "          'executedQty': '0.00000000',\n",
      "          'cummulativeQuoteQty': '0.00000000',\n",
      "          'status': 'NEW',\n",
      "          'timeInForce': 'GTC',\n",
      "          'type': 'LIMIT',\n",
      "          'side': 'BUY',\n",
      "          'stopPrice': '0.00000000',\n",
      "          'icebergQty': '0.00000000',\n",
      "          'time': 1706119355892,\n",
      "          'updateTime': 1706119355892,\n",
      "          'isWorking': True,\n",
      "          'workingTime': 1706119355892,\n",
      "          'origQuoteOrderQty': '0.00000000',\n",
      "          'selfTradePreventionMode': 'EXPIRE_MAKER'},\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "# 堵塞，等待订单完全成交，如果超时后仍未成交，返回订单数据\n",
    "orderId = set_order_result['data']['orderId']\n",
    "wait_order_filled_result = trade.wait_order_FILLED(\n",
    "    symbol='MANAUSDT',\n",
    "    orderId=orderId,\n",
    "    timeout=5,\n",
    ")\n",
    "\n",
    "eprint(wait_order_filled_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "401f5c7d",
   "metadata": {},
   "source": [
    "## 9. 撤销订单（API原始接口） cancel_order"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "7880b402",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': {'symbol': 'MANAUSDT',\n",
      "          'origClientOrderId': 'P3PQXqOCmLmvSgT3bHpNhY',\n",
      "          'orderId': 2207382482,\n",
      "          'orderListId': -1,\n",
      "          'clientOrderId': 'Gll4JiBPwxkI3FYhIB0CHi',\n",
      "          'transactTime': 1706119384094,\n",
      "          'price': '0.40000000',\n",
      "          'origQty': '15.00000000',\n",
      "          'executedQty': '0.00000000',\n",
      "          'cummulativeQuoteQty': '0.00000000',\n",
      "          'status': 'CANCELED',\n",
      "          'timeInForce': 'GTC',\n",
      "          'type': 'LIMIT',\n",
      "          'side': 'BUY',\n",
      "          'selfTradePreventionMode': 'EXPIRE_MAKER'},\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "orderId = set_order_result['data']['orderId']\n",
    "cancel_order_result = trade.cancel_order(\n",
    "    symbol='MANAUSDT',\n",
    "    orderId=orderId,\n",
    ")\n",
    "eprint(cancel_order_result)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
